package com.cms.model;

import com.cms.plugin.db.Model;
import com.cms.plugin.db.Page;
import com.cms.plugin.db.annotation.Table;


/**
 * Admin model.

将表结构放在此，消除记忆负担
mysql> desc s_admin;

数据库字段名建议使用驼峰命名规则，便于与 java 代码保持一致，如字段名： userId
 */
@SuppressWarnings("serial")
@Table("s_admin")
public class Admin extends Model<Admin>{
	public static final Admin dao = new Admin();
	
	/**
	 * 所有 sql 写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
	 */
	public Page<Admin> paginate(int pageNumber, int pageSize) {
		return paginate(pageNumber, pageSize, "select *", "from s_admin order by id desc");
	}

	public int getRoleById(String aid) {
		Admin a=findFirst("select role_id from s_role_admin where admin_id=?",aid);
		return a.getInt("role_id");
	}

	public boolean grant(String rid, String aid) {
		update("delete from s_role_admin where admin_id=?", aid);
		int[] batchInsert = batchInsert("insert into s_role_admin(admin_id,role_id) values(?,?)", new Object[][]{{aid,rid}});
		if(batchInsert.length>0){
			return true;
		}
		return false;
	}
	
	public Admin findByLoginName(String loginName){
		return findFirst("select id from s_admin where loginName=?", loginName);
	}

}
